//
//  DateUtil.m
//  hupan
//
//  Created by Li Zheng Qing on 11-7-1.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DateUtil.h"
#import <Foundation/NSCalendar.h>
#import <Foundation/NSDate.h>

@implementation DateUtil

/*
 + (NSString *) getTimeString:(NSTimeInterval) time{
    
    NSDate *now = [NSDate date];
    NSDate *postTime = [NSDate dateWithTimeIntervalSince1970:(time)];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
    
    NSUInteger unitFlags =  NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDateComponents *components = [calendar components:unitFlags fromDate:postTime toDate:now options:0];
    
    [calendar release];

    NSString *string;
    NSInteger years = [components year];
    NSInteger months = [components month];
    if (years > 0 || months > 0) {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        string = [dateFormatter stringFromDate:postTime];
        [dateFormatter release];
        return string;
        
    }
    
    NSInteger days = [components day];
    if(days > 0){
        
        string = [NSString stringWithFormat:@"%d天前", days];
        return string;
        
    }
    
    NSInteger hours = [components hour];
    if(hours > 0){
        
        string = [NSString stringWithFormat:@"%d小时前", hours];
        return string;
        
    }
    
    NSInteger minutes = [components minute];
    if(minutes > 0){
        
        string = [NSString stringWithFormat:@"%d分钟前", minutes];
        return string;
        
    }
    
    NSInteger seconds = [components second];
    if(seconds > 0){
        
        string = [NSString stringWithFormat:@"%d秒前", seconds];
        return string;
        
    }
    
    return @"刚刚";
    
}*/

+ (NSString *) getTimeString:(NSTimeInterval) time{
    
    // Calculate distance time string
    //
    
    int distance = [[NSDate date] timeIntervalSince1970] - time;
    NSString *string;
    if (distance < 1){//avoid 0 seconds
        string = @"刚刚";
    }
    else if (distance < 60) {
        string = [NSString stringWithFormat:@"%d秒前", (distance)];
    }
    else if (distance < 3600) {//60 * 60
        distance = distance / 60;
        string = [NSString stringWithFormat:@"%d分钟前", (distance)];
    }  
    else if (distance < 86400) {//60 * 60 * 24
        distance = distance / 3600;
        string = [NSString stringWithFormat:@"%d小时前", (distance)];
    }
    else if (distance < 604800) {//60 * 60 * 24 * 7
        distance = distance / 86400;
        string = [NSString stringWithFormat:@"%d天前", (distance)];
    }
    else if (distance < 2419200) {//60 * 60 * 24 * 7 * 4
        distance = distance / 604800;
        string = [NSString stringWithFormat:@"%d周前", (distance)];
    }
    else {
        static NSDateFormatter *dateFormatter = nil;
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        }

        string = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(time)]];

    }
    return string;
}
@end
